using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Text;
using ExceptionHandler;
using System.Configuration;
using System.Diagnostics;
using Microsoft.ApplicationBlocks.ExceptionManagement;
namespace ASUV
{
    public class ASConfigForm : ASUV.ASChildForm
	{
		#region [members]
		private GroupBox newProjectGroupBox;
        private Button createProjectButton;
        private Button browseNewButton;
        private Label projectNameLabel;
        private Label projectFileLabel;
        private Label projectLocationLabel;
        private TextBox projectNameTextBox;
        private TextBox projectFileNameTextBox;
        private TextBox projectLocationTextBox;
        private FolderBrowserDialog plFolderBrowserDialog;
        private GroupBox configGroupBox;
        private Label databaseNameLabel;
        private TextBox databaseNameTextBox;
        private Button testconn2005Button;
        private Button testConn2000Button;
        private Button cancelButton;
        private Button applyButton;
        private Label as2005Label;
        private Label as2000Label;
        private TextBox as2005TextBox;
        private TextBox as2000TextBox;
        private GroupBox openProjectGroupBox;
        private Button openProjectButton;
        private Button browseOpenButton;
        private Label selectProjectLabel;
        private TextBox selectProjectTextBox;
        private OpenFileDialog pfOpenFileDialog;
        private System.ComponentModel.IContainer components = null;
		ProjectFileFoundEventArgs projectDet;
		bool applyStatus,openProjectStatus,selectLocationStatus;
		#endregion

		#region public ASConfigForm()
		public ASConfigForm()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
            SetState(false);

            // TODO: Add any initialization after the InitializeComponent call
		}
		#endregion

		#region public ASConfigForm(ProjectFileFoundEventArgs projectDetails)
		public ASConfigForm(ProjectFileFoundEventArgs projectDetails)
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
            projectDet = projectDetails;
            SetState(true);

            // TODO: Add any initialization after the InitializeComponent call
		}
		#endregion

		#region protected override void Dispose(bool disposing)
		/// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
		}
		#endregion

		#region Designer generated code
		/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			this.newProjectGroupBox = new System.Windows.Forms.GroupBox();
			this.createProjectButton = new System.Windows.Forms.Button();
			this.browseNewButton = new System.Windows.Forms.Button();
			this.projectNameLabel = new System.Windows.Forms.Label();
			this.projectFileLabel = new System.Windows.Forms.Label();
			this.projectLocationLabel = new System.Windows.Forms.Label();
			this.projectNameTextBox = new System.Windows.Forms.TextBox();
			this.projectFileNameTextBox = new System.Windows.Forms.TextBox();
			this.projectLocationTextBox = new System.Windows.Forms.TextBox();
			this.plFolderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
			this.configGroupBox = new System.Windows.Forms.GroupBox();
			this.databaseNameLabel = new System.Windows.Forms.Label();
			this.databaseNameTextBox = new System.Windows.Forms.TextBox();
			this.testconn2005Button = new System.Windows.Forms.Button();
			this.testConn2000Button = new System.Windows.Forms.Button();
			this.cancelButton = new System.Windows.Forms.Button();
			this.applyButton = new System.Windows.Forms.Button();
			this.as2005Label = new System.Windows.Forms.Label();
			this.as2000Label = new System.Windows.Forms.Label();
			this.as2005TextBox = new System.Windows.Forms.TextBox();
			this.as2000TextBox = new System.Windows.Forms.TextBox();
			this.openProjectGroupBox = new System.Windows.Forms.GroupBox();
			this.selectProjectLabel = new System.Windows.Forms.Label();
			this.openProjectButton = new System.Windows.Forms.Button();
			this.browseOpenButton = new System.Windows.Forms.Button();
			this.selectProjectTextBox = new System.Windows.Forms.TextBox();
			this.pfOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.newProjectGroupBox.SuspendLayout();
			this.configGroupBox.SuspendLayout();
			this.openProjectGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// newProjectGroupBox
			// 
			this.newProjectGroupBox.Controls.Add(this.createProjectButton);
			this.newProjectGroupBox.Controls.Add(this.browseNewButton);
			this.newProjectGroupBox.Controls.Add(this.projectNameLabel);
			this.newProjectGroupBox.Controls.Add(this.projectFileLabel);
			this.newProjectGroupBox.Controls.Add(this.projectLocationLabel);
			this.newProjectGroupBox.Controls.Add(this.projectNameTextBox);
			this.newProjectGroupBox.Controls.Add(this.projectFileNameTextBox);
			this.newProjectGroupBox.Controls.Add(this.projectLocationTextBox);
			this.newProjectGroupBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.newProjectGroupBox.Location = new System.Drawing.Point(15, 12);
			this.newProjectGroupBox.Name = "newProjectGroupBox";
			this.newProjectGroupBox.Size = new System.Drawing.Size(618, 158);
			this.newProjectGroupBox.TabIndex = 2;
			this.newProjectGroupBox.TabStop = false;
			this.newProjectGroupBox.Text = "New Project";
			// 
			// createProjectButton
			// 
			this.createProjectButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.createProjectButton.Location = new System.Drawing.Point(472, 123);
			this.createProjectButton.Name = "createProjectButton";
			this.createProjectButton.Size = new System.Drawing.Size(95, 23);
			this.createProjectButton.TabIndex = 4;
			this.createProjectButton.Text = "Create Project";
			this.createProjectButton.Click += new System.EventHandler(this.createProjectButton_Click);
			// 
			// browseNewButton
			// 
			this.browseNewButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.browseNewButton.Location = new System.Drawing.Point(472, 28);
			this.browseNewButton.Name = "browseNewButton";
			this.browseNewButton.Size = new System.Drawing.Size(95, 23);
			this.browseNewButton.TabIndex = 1;
			this.browseNewButton.Text = "Browse";
			this.browseNewButton.Click += new System.EventHandler(this.browseNewButton_Click);
			// 
			// projectNameLabel
			// 
			this.projectNameLabel.AutoSize = true;
			this.projectNameLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.projectNameLabel.Location = new System.Drawing.Point(42, 95);
			this.projectNameLabel.Name = "projectNameLabel";
			this.projectNameLabel.Size = new System.Drawing.Size(70, 13);
			this.projectNameLabel.TabIndex = 12;
			this.projectNameLabel.Text = "Project Name:";
			// 
			// projectFileLabel
			// 
			this.projectFileLabel.AutoSize = true;
			this.projectFileLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.projectFileLabel.Location = new System.Drawing.Point(42, 60);
			this.projectFileLabel.Name = "projectFileLabel";
			this.projectFileLabel.Size = new System.Drawing.Size(89, 13);
			this.projectFileLabel.TabIndex = 11;
			this.projectFileLabel.Text = "Project File Name:";
			// 
			// projectLocationLabel
			// 
			this.projectLocationLabel.AutoSize = true;
			this.projectLocationLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.projectLocationLabel.Location = new System.Drawing.Point(42, 26);
			this.projectLocationLabel.Name = "projectLocationLabel";
			this.projectLocationLabel.Size = new System.Drawing.Size(83, 13);
			this.projectLocationLabel.TabIndex = 10;
			this.projectLocationLabel.Text = "Project Location:";
			// 
			// projectNameTextBox
			// 
			this.projectNameTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.projectNameTextBox.Location = new System.Drawing.Point(212, 95);
			this.projectNameTextBox.MaxLength = 30;
			this.projectNameTextBox.Name = "projectNameTextBox";
			this.projectNameTextBox.Size = new System.Drawing.Size(245, 20);
			this.projectNameTextBox.TabIndex = 3;
			// 
			// projectFileNameTextBox
			// 
			this.projectFileNameTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.projectFileNameTextBox.Location = new System.Drawing.Point(212, 60);
			this.projectFileNameTextBox.MaxLength = 30;
			this.projectFileNameTextBox.Name = "projectFileNameTextBox";
			this.projectFileNameTextBox.Size = new System.Drawing.Size(245, 20);
			this.projectFileNameTextBox.TabIndex = 2;
			this.projectFileNameTextBox.Leave += new System.EventHandler(this.projectFileNameTextBox_Leave);
			// 
			// projectLocationTextBox
			// 
			this.projectLocationTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.projectLocationTextBox.Location = new System.Drawing.Point(212, 28);
			this.projectLocationTextBox.Name = "projectLocationTextBox";
			this.projectLocationTextBox.ReadOnly = true;
			this.projectLocationTextBox.Size = new System.Drawing.Size(245, 20);
			this.projectLocationTextBox.TabIndex = 7;
			// 
			// configGroupBox
			// 
			this.configGroupBox.Controls.Add(this.databaseNameLabel);
			this.configGroupBox.Controls.Add(this.databaseNameTextBox);
			this.configGroupBox.Controls.Add(this.testconn2005Button);
			this.configGroupBox.Controls.Add(this.testConn2000Button);
			this.configGroupBox.Controls.Add(this.cancelButton);
			this.configGroupBox.Controls.Add(this.applyButton);
			this.configGroupBox.Controls.Add(this.as2005Label);
			this.configGroupBox.Controls.Add(this.as2000Label);
			this.configGroupBox.Controls.Add(this.as2005TextBox);
			this.configGroupBox.Controls.Add(this.as2000TextBox);
			this.configGroupBox.Enabled = false;
			this.configGroupBox.Location = new System.Drawing.Point(15, 195);
			this.configGroupBox.Name = "configGroupBox";
			this.configGroupBox.Size = new System.Drawing.Size(618, 194);
			this.configGroupBox.TabIndex = 3;
			this.configGroupBox.TabStop = false;
			// 
			// databaseNameLabel
			// 
			this.databaseNameLabel.AutoSize = true;
			this.databaseNameLabel.Location = new System.Drawing.Point(42, 35);
			this.databaseNameLabel.Name = "databaseNameLabel";
			this.databaseNameLabel.Size = new System.Drawing.Size(83, 13);
			this.databaseNameLabel.TabIndex = 12;
			this.databaseNameLabel.Text = "Database Name:";
			// 
			// databaseNameTextBox
			// 
			this.databaseNameTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.databaseNameTextBox.Location = new System.Drawing.Point(212, 29);
			this.databaseNameTextBox.MaxLength = 50;
			this.databaseNameTextBox.Name = "databaseNameTextBox";
			this.databaseNameTextBox.Size = new System.Drawing.Size(245, 20);
			this.databaseNameTextBox.TabIndex = 1;
			this.databaseNameTextBox.Leave += new System.EventHandler(this.databaseNameTextBox_Leave);
			this.databaseNameTextBox.TextChanged += new System.EventHandler(this.databaseNameTextBox_TextChanged);
			// 
			// testconn2005Button
			// 
			this.testconn2005Button.Location = new System.Drawing.Point(472, 108);
			this.testconn2005Button.Name = "testconn2005Button";
			this.testconn2005Button.Size = new System.Drawing.Size(95, 23);
			this.testconn2005Button.TabIndex = 5;
			this.testconn2005Button.Text = "Test Connection";
			this.testconn2005Button.Click += new System.EventHandler(this.testconn2005Button_Click);
			// 
			// testConn2000Button
			// 
			this.testConn2000Button.Location = new System.Drawing.Point(472, 65);
			this.testConn2000Button.Name = "testConn2000Button";
			this.testConn2000Button.Size = new System.Drawing.Size(95, 23);
			this.testConn2000Button.TabIndex = 3;
			this.testConn2000Button.Text = "Test Connection";
			this.testConn2000Button.Click += new System.EventHandler(this.testConn2000Button_Click);
			// 
			// cancelButton
			// 
			this.cancelButton.Location = new System.Drawing.Point(472, 145);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.Size = new System.Drawing.Size(95, 23);
			this.cancelButton.TabIndex = 7;
			this.cancelButton.Text = "Cancel";
			this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
			// 
			// applyButton
			// 
			this.applyButton.Location = new System.Drawing.Point(325, 145);
			this.applyButton.Name = "applyButton";
			this.applyButton.Size = new System.Drawing.Size(95, 23);
			this.applyButton.TabIndex = 6;
			this.applyButton.Text = "Apply";
			this.applyButton.Click += new System.EventHandler(this.applyButton_Click);
			// 
			// as2005Label
			// 
			this.as2005Label.AutoSize = true;
			this.as2005Label.Location = new System.Drawing.Point(42, 108);
			this.as2005Label.Name = "as2005Label";
			this.as2005Label.Size = new System.Drawing.Size(159, 13);
			this.as2005Label.TabIndex = 5;
			this.as2005Label.Text = "Analysis Services 2005 Instance:";
			// 
			// as2000Label
			// 
			this.as2000Label.AutoSize = true;
			this.as2000Label.Location = new System.Drawing.Point(42, 71);
			this.as2000Label.Name = "as2000Label";
			this.as2000Label.Size = new System.Drawing.Size(159, 13);
			this.as2000Label.TabIndex = 4;
			this.as2000Label.Text = "Analysis Services 2000 Instance:";
			// 
			// as2005TextBox
			// 
			this.as2005TextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.as2005TextBox.Location = new System.Drawing.Point(212, 106);
			this.as2005TextBox.MaxLength = 50;
			this.as2005TextBox.Name = "as2005TextBox";
			this.as2005TextBox.Size = new System.Drawing.Size(245, 20);
			this.as2005TextBox.TabIndex = 4;
			this.as2005TextBox.Text = "<SQL Server 2005  Name\\Instance Name>";
			this.as2005TextBox.Enter += new System.EventHandler(this.as2005TextBox_Enter);
			this.as2005TextBox.Leave += new System.EventHandler(this.as2005TextBox_Leave);
			this.as2005TextBox.TextChanged += new System.EventHandler(this.as2005TextBox_TextChanged);
			// 
			// as2000TextBox
			// 
			this.as2000TextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.as2000TextBox.Location = new System.Drawing.Point(212, 65);
			this.as2000TextBox.MaxLength = 50;
			this.as2000TextBox.Name = "as2000TextBox";
			this.as2000TextBox.Size = new System.Drawing.Size(245, 20);
			this.as2000TextBox.TabIndex = 2;
			this.as2000TextBox.Text = "<SQL Server 2000  Name\\Instance Name>";
			this.as2000TextBox.Enter += new System.EventHandler(this.as2000TextBox_Enter);
			this.as2000TextBox.Leave += new System.EventHandler(this.as2000TextBox_Leave);
			this.as2000TextBox.TextChanged += new System.EventHandler(this.as2000TextBox_TextChanged);
			// 
			// openProjectGroupBox
			// 
			this.openProjectGroupBox.Controls.Add(this.selectProjectLabel);
			this.openProjectGroupBox.Controls.Add(this.openProjectButton);
			this.openProjectGroupBox.Controls.Add(this.browseOpenButton);
			this.openProjectGroupBox.Controls.Add(this.selectProjectTextBox);
			this.openProjectGroupBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.openProjectGroupBox.Location = new System.Drawing.Point(15, 12);
			this.openProjectGroupBox.Name = "openProjectGroupBox";
			this.openProjectGroupBox.Size = new System.Drawing.Size(618, 115);
			this.openProjectGroupBox.TabIndex = 4;
			this.openProjectGroupBox.TabStop = false;
			this.openProjectGroupBox.Text = "Open Project";
			// 
			// selectProjectLabel
			// 
			this.selectProjectLabel.AutoSize = true;
			this.selectProjectLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.selectProjectLabel.Location = new System.Drawing.Point(42, 40);
			this.selectProjectLabel.Name = "selectProjectLabel";
			this.selectProjectLabel.Size = new System.Drawing.Size(91, 13);
			this.selectProjectLabel.TabIndex = 10;
			this.selectProjectLabel.Text = "Select Project File:";
			// 
			// openProjectButton
			// 
			this.openProjectButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.openProjectButton.Location = new System.Drawing.Point(472, 86);
			this.openProjectButton.Name = "openProjectButton";
			this.openProjectButton.Size = new System.Drawing.Size(95, 23);
			this.openProjectButton.TabIndex = 2;
			this.openProjectButton.Text = "Open Project";
			this.openProjectButton.Click += new System.EventHandler(this.openProjectButton_Click);
			// 
			// browseOpenButton
			// 
			this.browseOpenButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.browseOpenButton.Location = new System.Drawing.Point(472, 41);
			this.browseOpenButton.Name = "browseOpenButton";
			this.browseOpenButton.Size = new System.Drawing.Size(95, 23);
			this.browseOpenButton.TabIndex = 1;
			this.browseOpenButton.Text = "Browse";
			this.browseOpenButton.Click += new System.EventHandler(this.browseOpenButton_Click);
			// 
			// selectProjectTextBox
			// 
			this.selectProjectTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.selectProjectTextBox.Location = new System.Drawing.Point(212, 41);
			this.selectProjectTextBox.Name = "selectProjectTextBox";
			this.selectProjectTextBox.ReadOnly = true;
			this.selectProjectTextBox.Size = new System.Drawing.Size(245, 20);
			this.selectProjectTextBox.TabIndex = 7;
			// 
			// pfOpenFileDialog
			// 
			this.pfOpenFileDialog.Filter = "XML Files | *.xml";
			// 
			// ASConfigForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.ClientSize = new System.Drawing.Size(701, 588);
			this.Controls.Add(this.configGroupBox);
			this.Controls.Add(this.openProjectGroupBox);
			this.Controls.Add(this.newProjectGroupBox);
			this.Location = new System.Drawing.Point(0, 0);
			this.Name = "ASConfigForm";
			this.Text = " ";
			this.TextChanged += new System.EventHandler(this.ASConfigForm_TextChanged);
			this.newProjectGroupBox.ResumeLayout(false);
			this.newProjectGroupBox.PerformLayout();
			this.configGroupBox.ResumeLayout(false);
			this.configGroupBox.PerformLayout();
			this.openProjectGroupBox.ResumeLayout(false);
			this.openProjectGroupBox.PerformLayout();
			this.ResumeLayout(false);

		}
        #endregion

		#region private void SetState(bool flag)
		/// <summary>
		/// Set the Form text according to the type of window(New,Open or Configuration loading to the project
		/// </summary>
		/// <param name="flag"></param>
        private void SetState(bool flag)
        {
            string formText = this.Text;
            switch (formText)
            {
                case "Configuration":
                    newProjectGroupBox.Text ="Project Details";
                    configGroupBox.Enabled = true;
                    newProjectGroupBox.Enabled = false;
                    openProjectGroupBox.Visible = false;
                    if (flag && projectDet !=null)
                    {
                        SetProjectDet(); 
                    }
                    break;
                case "New Project":
                    newProjectGroupBox.Text = "New Project";
                    openProjectGroupBox.Visible = false;
                    break;
                case "Open Project":
                    configGroupBox.Enabled = false;
                    newProjectGroupBox.Visible = false;
                    openProjectGroupBox.Visible = true;
                    break;
                default:
                    break;
            }
		}
		#endregion

		#region private void projectFileNameTextBox_Leave(object sender, EventArgs e)
		private void projectFileNameTextBox_Leave(object sender, EventArgs e)
        {
            projectNameTextBox.Text = projectFileNameTextBox.Text;
		}
		#endregion

		# region FileFoundEvent
		public event EventHandler<ProjectFileFoundEventArgs> FileFound;
        protected virtual void OnFileFound()
        {
            if (FileFound != null) FileFound(this, projectDet);
        }
        # endregion

		#region private void ASConfigForm_TextChanged(object sender, EventArgs e)
		private void ASConfigForm_TextChanged(object sender, EventArgs e)
        {
            SetState(true);
		}
		#endregion

		#region private void browseOpenButton_Click(object sender, EventArgs e)
		private void browseOpenButton_Click(object sender, EventArgs e)
        {
            pfOpenFileDialog.ShowDialog();
            selectProjectTextBox.Text = pfOpenFileDialog.FileName.ToString();
			if (selectProjectTextBox.Text.Trim().Length != 0)
			{
				openProjectStatus = true;
			}
		}
		#endregion

		#region private void createProjectButton_Click(object sender, EventArgs e)
		/// <summary>
		/// Create project file in xml format with basic structure
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

       private void createProjectButton_Click(object sender, EventArgs e)
        {
			try
			{
				projectNameTextBox.Text = projectNameTextBox.Text;
				projectNameTextBox.Text = projectNameTextBox.Text;
				if (selectLocationStatus == true)
				{
					if (projectFileNameTextBox.Text.Trim().Length == 0)
					{
						projectFileNameTextBox.Focus();
						throw new CustomAppException("customException8");
					}
					else if (projectFileNameTextBox.Text.Contains("<") || projectFileNameTextBox.Text.Contains(">") || projectFileNameTextBox.Text.Contains("*") || projectFileNameTextBox.Text.Contains("\""))
					{
						projectFileNameTextBox.Focus();
						projectFileNameTextBox.Text = "";
						projectNameTextBox.Text = "";
						throw new CustomAppException("customException5");
					}
					if (projectNameTextBox.Text.Trim().Length == 0)
					{
						projectNameTextBox.Focus();
						throw new CustomAppException("customException9");
					}
					projectDet = new ProjectFileFoundEventArgs();
					projectFileNameTextBox.Text += ".xml";
					projectDet.ProjectLocation = projectLocationTextBox.Text;
					projectDet.ProjectFileName = projectFileNameTextBox.Text;
					projectDet.ProjectName = projectNameTextBox.Text;
					projectDet.AS2000 = "";
					projectDet.AS2005 = "";
					projectDet.DatabaseName = "";
					if (ProjectFile.CreateProject(projectDet) == "File Created")
					{
						OnFileFound();
						newProjectGroupBox.Text = "Project Details";
						newProjectGroupBox.Enabled = false;
						configGroupBox.Enabled = true;
						databaseNameTextBox.Focus();
						throw new CustomAppException("customException12");
					}
					else
					{
						projectFileNameTextBox.Text = "";
						projectNameTextBox.Text = "";
						projectFileNameTextBox.Focus();
						throw new CustomAppException("customException6");
					}
				}
				else
				{
					throw new CustomAppException("customException7");
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private void openProjectButton_Click(object sender, EventArgs e)
		/// <summary>
		/// Open existing project and load project details
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void openProjectButton_Click(object sender, EventArgs e)
        {
			try
			{
				if (openProjectStatus == true)
				{
					projectDet = new ProjectFileFoundEventArgs();
					projectDet.ProjectLocation = Path.GetDirectoryName(pfOpenFileDialog.FileName);
					projectDet.ProjectFileName = Path.GetFileName(pfOpenFileDialog.FileName);
					XmlDocument xmlDom = ProjectFile.GetProjectDetails(projectDet.ProjectLocation + "\\" + projectDet.ProjectFileName);
					projectDet.ProjectName = xmlDom.SelectSingleNode("ProjectFile/ProjectName").InnerText.Replace("amp;", "&");
					projectDet.ProjectName = projectDet.ProjectName.Replace("quot;", "\"");
					projectDet.DatabaseName = xmlDom.SelectSingleNode("ProjectFile/DatabaseName").InnerText.Replace("amp;", "&");
					projectDet.DatabaseName = projectDet.DatabaseName.Replace("quot;", "\"");
					projectDet.AS2000 = xmlDom.SelectSingleNode("ProjectFile/AS2K").InnerText.Replace("amp;", "&");
					projectDet.AS2000 = projectDet.AS2000.Replace("quot;", "\"");
					projectDet.AS2005 = xmlDom.SelectSingleNode("ProjectFile/AS2K5").InnerText.Replace("amp;", "&");
					projectDet.AS2005 = projectDet.AS2005.Replace("quot;", "\"");
					OnFileFound();
					newProjectGroupBox.Text = "Project Details";
					newProjectGroupBox.Enabled = false;
					newProjectGroupBox.Visible = true;
					openProjectGroupBox.Visible = false;
					configGroupBox.Enabled = true;
					SetProjectDet();
					databaseNameTextBox.Focus();
				}
				else
				{
					browseNewButton.Focus();
					throw new CustomAppException("fileNotFound");

				}
			}
			catch (NullReferenceException)
			{
				openProjectStatus = false;
				selectProjectTextBox.Text = "";
				try
				{
					throw new CustomAppException("customException19");
				}
				catch (CustomAppException ex)
				{
					ExceptionManager.Publish(ex);
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private void browseNewButton_Click(object sender, EventArgs e)
		private void browseNewButton_Click(object sender, EventArgs e)
        {
            plFolderBrowserDialog.ShowDialog();
            projectLocationTextBox.Text = plFolderBrowserDialog.SelectedPath.ToString();
			if (projectLocationTextBox.Text.Trim().Length != 0)
			{
				selectLocationStatus = true;
			}
		}
		#endregion

		#region private void SetProjectDet()
		private void SetProjectDet()
        {
            projectLocationTextBox.Text = projectDet.ProjectLocation;
            projectFileNameTextBox.Text = projectDet.ProjectFileName.Replace("amp;","&");
            projectNameTextBox.Text = projectDet.ProjectName;
            databaseNameTextBox.Text = projectDet.DatabaseName;
            as2000TextBox.Text = projectDet.AS2000;
            as2005TextBox.Text = projectDet.AS2005;
		}
		#endregion

		#region private void applyButton_Click(object sender, EventArgs e)
		/// <summary>
		/// Save changes to the project file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void applyButton_Click(object sender, EventArgs e)
        {
			if (applyStatus == true)
			{
				try
				{
					XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDet.ProjectLocation + "\\" + projectDet.ProjectFileName);
					xmlDomProjectContent.SelectSingleNode("ProjectFile/DatabaseName").InnerText = databaseNameTextBox.Text.Trim().Replace("&", "amp;");
					xmlDomProjectContent.SelectSingleNode("ProjectFile/AS2K").InnerText = as2000TextBox.Text.Trim().Replace("&", "amp;");
					xmlDomProjectContent.SelectSingleNode("ProjectFile/AS2K5").InnerText = as2005TextBox.Text.Trim().Replace("&", "amp;");
					xmlDomProjectContent.SelectSingleNode("ProjectFile/LastModifiedDateTime").InnerText = DateTime.Now.ToString();
					if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries").InnerText.Trim().Length != 0)
					{

						foreach (XmlNode xmlNodeQuery in xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries"))
						{
							xmlNodeQuery.SelectSingleNode("DatabaseName").InnerText = databaseNameTextBox.Text.Trim().Replace("&", "amp;");
						}
					}
					ProjectFile.SetProjectDetails(xmlDomProjectContent, projectDet.ProjectLocation + "\\" + projectDet.ProjectFileName);
					projectDet.DatabaseName = databaseNameTextBox.Text.Trim();
					projectDet.AS2000 = as2000TextBox.Text.Trim();
					projectDet.AS2005 = as2005TextBox.Text.Trim();
					throw new CustomAppException("customException28");
				}
				catch (CustomAppException ex)
				{
					ExceptionManager.Publish(ex);
				}
				catch (Exception ex)
				{
					EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
				}
			}
		}
		#endregion

		#region private void cancelButton_Click(object sender, EventArgs e)
		private void cancelButton_Click(object sender, EventArgs e)
        {
			if (applyStatus == true)
			{
				DialogResult result = MessageBox.Show("Do you want to cancel this operation?", "AS Upgrade Verification Tool", MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
				if (result == DialogResult.Yes)
				{
					databaseNameTextBox.Text = projectDet.DatabaseName;
					as2000TextBox.Text = projectDet.AS2000;
					as2005TextBox.Text = projectDet.AS2005;
				}
				this.Show();
			}
		}
		#endregion

		#region private void testConn2000Button_Click(object sender, EventArgs e)
		/// <summary>
		/// Test the connectivity of Aanlysis Services 2000 server
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void testConn2000Button_Click(object sender, EventArgs e)
		{
			try
			{
				if (databaseNameTextBox.Text.Trim().Length == 0)
				{
					databaseNameTextBox.Focus();

					throw new CustomAppException("customException23");
				}
				if (as2000TextBox.Text.Trim().Length == 0)
				{
					as2000TextBox.Focus();
					throw new CustomAppException("customException24");
				}
				AnalysisService asConn = new AnalysisService();
				bool connStatus = asConn.TestConnection(as2000TextBox.Text.Trim(), databaseNameTextBox.Text.Trim());
				if (connStatus == true) throw new CustomAppException("customException20");
				else throw new CustomAppException("customException21");
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private void testconn2005Button_Click(object sender, EventArgs e)
		/// <summary>
		/// Test the connectivity of Analysis Services 2005 server
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void testconn2005Button_Click(object sender, EventArgs e)
		{
			try
			{
				if (as2005TextBox.Text.Trim().Length == 0)
				{
					as2005TextBox.Focus();
					throw new CustomAppException("customException25");
				}
				AnalysisService asConn = new AnalysisService();
				bool connStatus = asConn.TestConnection(as2005TextBox.Text.Trim().Replace("\"", ""), databaseNameTextBox.Text.Trim().Replace("\"", ""));
				if (connStatus == true) throw new CustomAppException("customException20");
				else throw new CustomAppException("customException21");
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private void databaseNameTextBox_TextChanged(object sender, EventArgs e)
		private void databaseNameTextBox_TextChanged(object sender, EventArgs e)
		{
			applyStatus = true;
		}
		#endregion

		#region private void as2000TextBox_TextChanged(object sender, EventArgs e)
		private void as2000TextBox_TextChanged(object sender, EventArgs e)
		{
			applyStatus = true;
		}
		#endregion

		#region private void as2005TextBox_TextChanged(object sender, EventArgs e)
		private void as2005TextBox_TextChanged(object sender, EventArgs e)
		{
			applyStatus = true;
		}
		#endregion

		#region private void as2000TextBox_Enter(object sender, EventArgs e)
		private void as2000TextBox_Enter(object sender, EventArgs e)
		{
			if (as2000TextBox.Text.Trim() == "<SQL Server 2000  Name\\Instance Name>")
			{
				as2000TextBox.Text = "";
			}
		}
		#endregion

		#region 
		private void as2005TextBox_Enter(object sender, EventArgs e)
		{
			if (as2005TextBox.Text.Trim() == "<SQL Server 2005  Name\\Instance Name>")
			{
				as2005TextBox.Text = "";
			}
		}
		#endregion

		#region private void databaseNameTextBox_Leave(object sender, EventArgs e)
		private void databaseNameTextBox_Leave(object sender, EventArgs e)
		{
			databaseNameTextBox.Text = databaseNameTextBox.Text.Replace("\"", "");
			databaseNameTextBox.Text = databaseNameTextBox.Text.Replace(";", "");
			databaseNameTextBox.Text = databaseNameTextBox.Text.Replace("<", "");
			databaseNameTextBox.Text = databaseNameTextBox.Text.Replace(">", "");
		}
		#endregion

		#region private void as2000TextBox_Leave(object sender, EventArgs e)
		private void as2000TextBox_Leave(object sender, EventArgs e)
		{
			as2000TextBox.Text = as2000TextBox.Text.Replace("\"", "");
			as2000TextBox.Text = as2000TextBox.Text.Replace(";", "");
			as2000TextBox.Text = as2000TextBox.Text.Replace("<", "");
			as2000TextBox.Text = as2000TextBox.Text.Replace(">", "");
		}
		#endregion

		#region private void as2005TextBox_Leave(object sender, EventArgs e)
		private void as2005TextBox_Leave(object sender, EventArgs e)
		{
			as2005TextBox.Text = as2005TextBox.Text.Replace("\"", "");
			as2005TextBox.Text = as2005TextBox.Text.Replace("<", "");
			as2005TextBox.Text = as2005TextBox.Text.Replace(";", "");
			as2005TextBox.Text = as2005TextBox.Text.Replace(">", "");
		}
		#endregion

	}

	# region "public class ProjectFileFoundEventArgs : EventArgs"

	/// <summary>
	/// Class to store the basic details of the current project
	/// </summary>
    public class ProjectFileFoundEventArgs : EventArgs
    {
        private string projectLocation;
        private string projectFileName;
        private string projectName;
        private string databaseName;
        private string as2000;
        private string as2005;

        public string ProjectLocation
        {
            get { return projectLocation; }
            set { projectLocation = value; }
        }
        public string ProjectFileName
        {
            get { return projectFileName; }
            set { projectFileName = value; }
        }
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
        public string DatabaseName
        {
            get { return databaseName; }
            set { databaseName = value; }
        }
        public string AS2000
        {
            get { return as2000; }
            set { as2000 = value; }
        }
        public string AS2005
        {
            get { return as2005; }
            set { as2005 = value; }
        }
	}
	#endregion
}

